package com.qing.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qing.aclservice.entity.pojo.AclPermission;
import com.qing.aclservice.entity.pojo.AclUser;
import com.qing.aclservice.service.AclPermissionService;
import com.qing.aclservice.service.AclUserService;
import com.qing.security.entity.SecurityUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName Yjx
 * @Description TODO
 * @date 2022/9/19 19:11
 * @Version 1.0
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AclUserService userService;

    @Autowired
    private AclPermissionService permissionService;

    /***
     * 根据账号获取用户信息
     * @param username:
     * @return: org.springframework.security.core.userdetails.UserDetails
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库中取出用户信息
        AclUser user = userService.getOne(new QueryWrapper<AclUser>().eq("username",username));

        // 判断用户是否存在
        if (null == user){
            //throw new UsernameNotFoundException("用户名不存在！");
        }
        // 返回UserDetails实现类
        AclUser curUser = new AclUser();
        BeanUtils.copyProperties(user,curUser);

        AclPermission authorities = permissionService.getOne(new QueryWrapper<AclPermission>().eq("id", user.getId()));
//        SecurityUser securityUser = new SecurityUser(curUser);
//        securityUser.setPermissionValueList(authorities.getPermissionValue());
//        return securityUser;
        return null;
    }

}
